import tkinter as tk
import random
from tkinter import *
import time as cur_time
from PIL import Image, ImageTk

# 箭头停止变量
toggleL = False
toggleR = False


class MainWindow(tk.Tk):
    def __init__(self, *args, **kw):
        super().__init__()
        self.geometry("730x400+700+400")  # 设置窗口长宽和坐标

        # 窗口标题
        self.title('Label')
        # 初始按钮，后面会被覆盖
        self.label1 = Label(self, text='', width=70, height=10)
        # 设置按钮位置
        self.label1.grid(row=0, column=1)
        # 显示图片并调整尺寸(注意这里默认支持的图片格式为GIF)
        image = Image.open('pictures/little_arrow.png')
        zoom = 0.2
        # multiple image size by zoom
        pixels_x, pixels_y = tuple([int(zoom * x) for x in image.size])
        self.images = (ImageTk.PhotoImage(image.resize((pixels_x, pixels_y))),
                       ImageTk.PhotoImage(Image.open('pictures/arrow1.png').resize((pixels_x, pixels_y))),
                       ImageTk.PhotoImage(Image.open('pictures/arrow1.png').resize((pixels_x, pixels_y))),
                       ImageTk.PhotoImage(Image.open('pictures/arrow1.png').resize((pixels_x, pixels_y))))

        self.labelArrow1 = Label(self, image=self.images[0], width=55, height=55)
        self.labelArrow1.place(x=200, y=300)
        self.labelArrow2 = Label(self, image=self.images[0], width=55, height=55)
        self.labelArrow2.place(x=300, y=300)
        # 调用after函数
        self.after(100, self.refresh_data)
        # 窗口显示
        self.mainloop()

    def refresh_data(self):
        print('我在刷新')
        global toggleL, toggleR
        toggleL = True
        self.change_arrow(0)
        # 需要刷新数据的操作，从time函数活的当前时间
        self.label1 = Label(self, text=cur_time.time(), font=18, relief="solid", width=25, height=5, padx=2)
        self.label1.grid(row=0, column=1, padx=15, pady=20)
        # 递归循环调用after
        self.after(100, self.refresh_data)  # 这里的100单位为毫秒

    def change_arrow(self, index):
        # 0，改变左边的箭头为绿色，1，改变右边的箭头为绿色
        if index == 0:
            if toggleL:
                self.labelArrow1.configure(image=self.images[int(cur_time.time() % 3)])
        else:
            if toggleR:
                self.labelArrow2.configure(image=self.images[int(cur_time.time() % 3)])


if __name__ == '__main__':
    MainWindow = MainWindow()
